<template>
    <div>
        <h4 class="font-weight-bold">
            <span class="text-muted font-weight-light">风控工具 /</span> 打分卡
        </h4>
        <b-card class="mb-4">
            <Row>
                <i-col span="7">
                    <b-input-group style="width:300px;">
                        <b-input type="text" placeholder="输入搜索的打分卡名称" />
                        <b-btn slot="append" class="btn-success">搜索</b-btn>
                    </b-input-group>
                </i-col>
                <i-col span="1">
                    <Button type="primary" shape="circle" icon="plus" @click="addSoreCard()">新增</Button>
                </i-col>
            </Row>
            <div>
                <Table :data="table.data" :columns="table.columns" :loading="table.loading" stripe border class="mt-2"></Table>
                <Page show-elevator show-total size="small" class="mt-2" :total="table.total" :current="table.currentPage"
                        @on-change="(pageNum) => {
                            let self = this;
                            table.pageChange.call(self,pageNum);
                        }"></Page>
            </div>
        </b-card>

    </div>
</template>


<script>
export default {
    name: "riskToolScorecardHome",
    metaInfo: {
        title: "风控工具 - 打分卡"
    },
    components: {},
    data() {
        return {
            oid: null,
            formInline: {},
            table: {
                showIndex: false,
                data: [],
                columns: [
                    {
                        title: "打分卡名称",
                        key: "name"
                    },
                    {
                        title: "应用对象",
                        key: "subjectCode",
                        render: (h, params) => {
                            let vm = this;
                            return (
                                <div>
                                    {params.row.apiMasterOid}
                                </div>
                            )
                        }
                    },
                    {
                        title: "基本分数",
                        key: "baseScore"
                    },
                    {
                        title: "备注",
                        key: "remark"
                    },
                    {
                        title: "状态",
                        key: "enable",
                        render: (h, context) => {
                            var isEnable = "已禁用";
                            // console.log(context)
                            if (context.row.enable) {
                                isEnable = "已启用";
                            }
                            return h("div", isEnable);
                        }
                    },
                    {
                        title: "操作",
                        width: 160,
                        align: "center",
                        render: (h, params) => {
                            let vm = this;
                            return (
                                <div>
                                    <i-button
                                        type="text"
                                        size="small"
                                        class="mr-2"
                                        shape="circle"
                                        icon="edit"
                                        title="编辑打分卡"
                                        onClick={() => {
                                            return vm.edit(params);
                                        }}
                                    />
                                    <i-button
                                        type="text"
                                        size="small"
                                        class="mr-2"
                                        shape="circle"
                                        icon="trash-b"
                                        title="删除打分卡"
                                        onClick={() => {
                                            return vm.remove(params);
                                        }}
                                    />
                                </div>
                            );
                        }
                    }
                ],
                // 是否载入中
                loading: false,
                // 总页数
                total: 0,
                // 当前页数
                currentPage : 1,
                // 每页条目数
                pageSize : 10,
                // 分页切换
                pageChange: function(pageNum){
                    let self = this;
                    self.table.currentPage = pageNum;
                    self.loadTable().then(function(res){
                        if(res.data){
                            self.table.data = res.data.data ? res.data.data : [];
                        }
                    });
                }
            }
        };
    },
    mounted(){
        let self = this;
        //加载客户列表
        self.loadTable().then(function(res){
            // console.log(res);
            if(res.data){
                self.table.data = res.data.data ? res.data.data : [];
            }
        });
    },

    methods: {
        loadTable(){
            let self = this;
            self.table.loading = true;

            return new Promise(function(resolve,reject){
                self.$http
                    .get('/app/v1/riskTool/scoreCards',{
                        params: {
                            pageSize: 10,
                            currentPage: self.table.currentPage
                        }
                    })
                    .then(function(res) {
                        self.table.total = res.data.recordCount;
                        self.table.loading = false;
                        resolve(res);
                    },function(res){
                        self.table.loading = false;
                        reject(res);
                    });
            });
        },
        edit: function(p) {
            this.$router.push({
                path: "/riskTool/scorecard/detail",
                query: {
                    oid: p.row.oid
                }
            });
        },
        addSoreCard(row) {
            this.$router.push({
                path: "/riskTool/scorecard/add"
            });
        },
        remove(data) {
            let self = this;
            let name = data.row.name;

            // console.log(name);
            this.$Modal.confirm({
                title: "系统提示",
                content: "<p>确认删除“" + name + "”？</p>",
                onOk: () => {
                    
                    // 发送请求
                    self.$http
                        .delete(
                            "/app/v1/riskTool/scoreCards/" + data.row.oid
                        )
                        .then(function(res) {
                            if(res.code === 0){
                                self.$Message.success("删除成功!");
                                self.$Notice.success({
                                    title: '系统提示',
                                    desc: res.desc
                                });

                                //加载客户列表
                                self.table.data = [];
                                self.loadTable().then(function(res){
                                    if(res.data){
                                        self.table.data = res.data.data ? res.data.data : [];
                                    }
                                });
                            }else{
                                self.$Notice.error({
                                    title: '系统提示',
                                    desc: res.desc
                                });
                            }
                            
                        });
                },
                onCancel: () => {
                    this.$Message.info("取消删除");
                }
            });
        }
    }
};
</script>
